﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using System.Xml.Linq;
using System.Reflection.Metadata;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using Castle.Core.Resource;
using MailKit.Search;

namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 晶硅WMS_车牌信息更新
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TMStoWMSCarInfoChangeController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con_zh;
        public readonly string con_qh;
        public readonly string con_yb;
        public readonly string con_gz;


        public TMStoWMSCarInfoChangeController(ILogger<TMStoWMSChangeCarInfoRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            con_zh = _configuration.GetConnectionString("WMSDbConnection_ZH");
            con_qh = _configuration.GetConnectionString("WMSDbConnection");
            con_yb = _configuration.GetConnectionString("WMSDbConnection_YB");
            con_gz = _configuration.GetConnectionString("WMSDbConnection_GZ");
            _logger = loggerFactory.CreateLogger<TMStoWMSCarInfoChangeController>();
        }

        /// <summary>
        /// 晶硅WMS_车牌信息更新
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostWMSChangeCarInfo([FromBody] TMStoWMSChangeCarInfoRequestDto strparm)
        {
            string strMsg = "";
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
            TMStoWMSChangeCarInfoResponseDto re = new TMStoWMSChangeCarInfoResponseDto();
            string chage_no = strparm.chage_no;
            string waybill_no = strparm.waybill_no;
            string in_con = "";//目标WMS数据库
            try
            {
                if (chage_no == "")
                {
                    strMsg = "fy_code must be not null";
                }
                if (strparm.factory_code == "")
                {
                    strMsg = "factory_code must be not null";
                }
                else
                {
                    if (strparm.factory_code.Contains("zh"))
                    {
                        in_con = con_zh;
                    }
                    else
                    {
                        if (strparm.factory_code.Contains("qh"))
                        {
                            in_con = con_qh;
                        }
                        else
                        {
                            if (strparm.factory_code.Contains("sc"))
                            {
                                in_con = con_yb;
                            }
                            else
                            {
                                if (strparm.factory_code.Contains("gz"))
                                {

                                }
                                else
                                {
                                    strMsg = "factory_code=<" + strparm.factory_code + "> not find ";
                                }                                
                            }
                        }
                    }
                }

                if (strMsg == "")
                {                  
                    dic = new Dictionary<string, string>(); //查询公用dic
                    dic.Add("chage_no", chage_no);//查询对应物流计划
                    DataTable change_dt = MethodHelp.GetTableInfo("TMS0084", dic, con);//获取物流计划明细，订单数据源
                    if (change_dt.Rows.Count > 0)
                    {
                        dic = new Dictionary<string, string>(); //查询公用dic
                        dic.Add("shippingorder_no", waybill_no);//查询对应物流计划
                        DataTable fy_dt = MethodHelp.GetOutsideTableInfo("TMS0083", dic, in_con, con);//获取WMS运单信息
                        if (fy_dt.Rows.Count > 0)
                        {
                            dic = new Dictionary<string, string>();
                            dic.Add("car_no", change_dt.Rows[0]["car_no"].ToString());
                            dic.Add("driver_name", change_dt.Rows[0]["driver_name"].ToString());
                            dic.Add("driver_tel", change_dt.Rows[0]["driver_tel"].ToString());
                            dic.Add("identity_no", change_dt.Rows[0]["driver_idcard"].ToString());
                            dic.Add("shippingorder_no", waybill_no);
                            strMsg = MethodHelp.InsertOutsideIntoTable("TMS0085", dic, in_con, con);//更新WMS车牌信息

                        }
                        else
                        {
                            strMsg = "WMS找不到运单=<" + waybill_no + "> 相关信息，请确认后再试！ ";
                        }
                        
                    }
                    else
                    {
                        strMsg = "TMS找不到运单,换车申请=<" + chage_no + "> 相关信息，请确认后再试！ ";
                    }
                }
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

            if (strMsg == "")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;

            }


            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);

        }


    }
}
